package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 选取特定的构造器实例化对象
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Person p = new Person("李四",55);
        System.out.println(p);

        Class cls = Class.forName("reflect.Person");
//        cls.getConstructor();//不传参数时,获取的就是无参构造器
        Constructor c = cls.getConstructor(String.class,int.class);//Person(String,int)

        Object o = c.newInstance("王五",66);//new Person("李四",55);
        System.out.println(o);
    }
}







